package com.baiye.template.dao;

import com.baiye.template.pojo.Permission;
import com.baiye.template.pojo.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/9/7 17:08
 */
@Repository
public interface RoleDao {

    @Delete("delete from role where id = #{id}")
    int deleteById(int id);

    @Insert("insert into role(name,desc) values(#{name},#{desc})")
    int add(Role role);

    @Insert("insert into user_role(user_id,role_id) values(#{userId},#{roleId})")
    int addRoleToUser(@Param("userId") int userId, @Param("roleId")int roleId);

    @Insert("delete from user_role where user_id=#{userId} and role_id=#{roleId}")
    int deleteRoleToUser(@Param("userId") int userId, @Param("roleId")int roleId);

    @Select({"<script>" +
            "select * from role " +
            " <where> " +
//            "<if test='start!=null and size !=null'>id >=( select id from role order by id limit #{start},1 )</if>"+
            " <if test='role.id != null'>AND id=#{role.id}</if> " +
            " <if test='role.name != null'> AND name=#{role.name}</if> " +
            " </where> " +
            "</script>"})
    List<Role> getRole(@Param("role")Role role, @Param("start") Integer start, @Param("pageSize") Integer pageSize);

    @Select("select * from role where id in (select role_id from user_role where user_id=#{userId})")
    List<Role> getRoleByUserIdExist(Integer userId);

    @Select("select * from role where id not in (select role_id from user_role where user_id=#{userId})")
    List<Role> getRoleByUserIdNotExist(Integer userId);
    @Update("update role set name=#{name},`desc`=#{desc}")
    int updateRole(Role role);
}
